/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.wufang.ui;

import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.io.mxCodec;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxUtils;
import com.mxgraph.util.mxXmlUtils;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import org.dusin.weijiwufang.bo.StatusBean;
import org.dusin.weijiwufang.bo.VisitorBean;
import org.dusin.weijiwufang.service.StatusService;
import org.dusin.weijiwufang.service.VisitorService;
import org.dusin.weijiwufang.ui.action.DrawAction;
import org.dusin.weijiwufang.ui.action.PrintBlankTicket;
import org.dusin.weijiwufang.ui.action.StatusManageAction;
import org.dusin.weijiwufang.ui.action.UserManageAction;
import org.dusin.wufang.util.WuFangUtil;
import org.dusin.wufang.util.jgraph.MyJGraphUtil;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.w3c.dom.Document;


/**
 *
 * @author Admin
 */
public class MainFrame extends JFrame{
    //private  static DatabaseConnection dc = new DatabaseConnection();
    JTabbedPane tabbedPane = new JTabbedPane();
    private ClassPathXmlApplicationContext ctx;
    private VisitorService visitorService;    
    private VisitorBean currentUser;    
    
    private StatusService statusService;    
  //  private StatusBean currentStatus;    
    
    private int tabCount=1;   //主界面上目前标签的数目
            
    
    public MainFrame() {        
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        visitorService = (VisitorService)ctx.getBean("visitorService");
        statusService = (StatusService)ctx.getBean("statusService");
//        LoginDialog loginDialog = new LoginDialog(this,true);
//        loginDialog.setVisitorService(visitorService);
//        loginDialog.setVisible(true);
//        currentUser = loginDialog.getVisitor();
        
        currentUser = new VisitorBean();
        currentUser.setLoginName("zhang");
        initUI();
    }

    public final void initUI() {

        JMenuBar menubar = new JMenuBar();
        ImageIcon icon = new ImageIcon(getClass().getResource("/org/dusin/weijiwufang/image/door_out.png"));

        //ImageIcon iconSys = new ImageIcon(getClass().getResource("/org/dusin/weijiwufang/image/wrench.png"));
        JMenu optrTicket = new JMenu("操作票(Y)");
        optrTicket.setMnemonic(KeyEvent.VK_Y);
        JMenuItem  printBlankTicketMenuItem  = new JMenuItem("打印空白票");
        printBlankTicketMenuItem.addActionListener(new PrintBlankTicket());
        optrTicket.add(printBlankTicketMenuItem);
        menubar.add(optrTicket);
        
        JMenu sys = new JMenu("系统");
        sys.setIcon(new ImageIcon(getClass().getResource("/org/dusin/weijiwufang/image/wrench.png")));
        sys.setMnemonic(KeyEvent.VK_F);
        
        ImageIcon iconDraw = WuFangUtil.createImageIcon("/org/dusin/weijiwufang/image/group.png");
        JMenuItem eMenuDraw = new JMenuItem("绘图", iconDraw);
        eMenuDraw.setMnemonic(KeyEvent.VK_D);        
        //eMenuItem.setToolTipText("Exit application");
        eMenuDraw.addActionListener(new DrawAction(tabbedPane,visitorService));        
        sys.add(eMenuDraw);
        
                 
        ImageIcon iconUser = new ImageIcon(getClass().getResource("/org/dusin/weijiwufang/image/group.png"));
        JMenuItem eMenuUser = new JMenuItem("用户管理", iconUser);
        eMenuUser.setMnemonic(KeyEvent.VK_U);        
        //eMenuItem.setToolTipText("Exit application");
        eMenuUser.addActionListener(new UserManageAction(this,tabbedPane,visitorService));        
        sys.add(eMenuUser);
        
        
        ImageIcon iconStatus = new ImageIcon(getClass().getResource("/org/dusin/weijiwufang/image/group.png"));
        JMenuItem eMenuStatus = new JMenuItem("运行状态管理", iconStatus);
        eMenuStatus.setMnemonic(KeyEvent.VK_S);        
        //eMenuItem.setToolTipText("Exit application");
        eMenuStatus.addActionListener(new StatusManageAction(this,tabbedPane,statusService));        
        sys.add(eMenuStatus);
        
        sys.addSeparator();
        
        JMenuItem eMenuExit = new JMenuItem("退出", icon);
        eMenuExit.setMnemonic(KeyEvent.VK_C);
        //eMenuItem.setToolTipText("Exit application");        
        eMenuExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }

        });
        sys.add(eMenuExit);
        
        menubar.add(sys);

        setJMenuBar(menubar);
        this.setLayout(new BorderLayout());
        //工具栏设置
        JToolBar toolBar = new JToolBar("Still draggable");
        
        JLabel userNameLabel = new JLabel("用户名:"+currentUser.getLoginName());
        toolBar.add(userNameLabel);
        setPreferredSize(new Dimension(450, 130));
        //addButtons(toolBar);
        this.add(toolBar,BorderLayout.NORTH);
        
        
        //程序主工作区,tabbedPanel
        
        this.add(tabbedPane,BorderLayout.CENTER);
        
        JComponent panel1 = makeTextPanel("Panel #1");
        tabbedPane.addTab("电站电路图", WuFangUtil.createImageIcon("/org/dusin/weijiwufang/image/home.png"), panel1,
                "电站电路图");
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
        
        setTitle("泰利丰变电站微机五防操作系统v1.0");
        //setSize(800, 600);        
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setExtendedState(Frame.MAXIMIZED_BOTH);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MainFrame ex = new MainFrame();
                ex.setVisible(true);
            }
        });
    }
    
    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);       
        panel.setLayout(new GridLayout(1, 1));
        
        mxGraph graph = new mxGraph();
        graph.setConnectableEdges(false);
        graph.setCellsCloneable(false);
        Object parent = graph.getDefaultParent();
        mxStylesheet stylesheet = graph.getStylesheet();
        Hashtable<String, Object> style = new Hashtable<String, Object>();
        style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_RECTANGLE);
        style.put(mxConstants.STYLE_OPACITY, 50);
        style.put(mxConstants.STYLE_FONTCOLOR, "#774400");
        stylesheet.putCellStyle("ROUNDED", style);
        
        MyJGraphUtil.loadShape();
        
        graph.getModel().beginUpdate();
        //Document document = mxXmlUtils.parseXml(mxUtils.readFile("D:\\My Documents\\NetBeansProjects\\JgraphDemo\\a.mxe"));
        
        Document document;
        try {
            document = mxXmlUtils.parseXml(mxUtils.readFile("circuit/wufang.mxe"));
        } catch (IOException ex) {
            return panel;
        }
        
        mxCodec codec = new mxCodec(document);
        codec.decode(
                document.getDocumentElement(),
                graph.getModel());
            
        graph.setCellsLocked(true);
        
        graph.getModel().endUpdate();
        graph.setCellsEditable(false);
        mxGraphComponent graphComponent = new mxGraphComponent(graph);
        graphComponent.setConnectable(false);
        
        graphComponent.getGraphControl().addMouseListener(new GraphMouseHandler(graph));
        panel.add(graphComponent);
        return panel;
    }
}
